﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void menu()
{
	printf("************************\n");
	printf("*******   扫雷   *******\n");
	printf("*******  1.play  *******\n");
	printf("*******  0.exit  *******\n");
	printf("************************\n");
}

//初始化棋盘
void Init_Board(char arr[ROWS][COLS], int row, int col,char set)
{
	for (int i = 0;i < row;i++)
	{
		for (int j = 0;j < col;j++)
		{
			arr[i][j] = set;
		}
	}
}

//埋雷
void Set(char arr[ROWS][COLS], int row, int col,int mine)
{
	srand((unsigned int)time(NULL));
	while(mine)
	{
		int x = 1 + rand() % row;
		int y = 1 + rand() % col;
		if (arr[x][y] != '1')
		{
			arr[x][y] = '1';
			mine--;
		}
	}
}

//打印棋盘
void Print_Board(char arr[ROWS][COLS], int row, int col)
{
	for (int i = 0;i <= ROW;i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1;i <= row;i++)
	{
		printf("%d ", i);
		for (int j = 1;j <= col;j++)
		{
			printf("%c ",arr[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

//自动检查
void Check_mine_a(char arr[ROWS][COLS], char arr1[ROWS][COLS], int x, int y,int row,int col, int mine)
{
	if (x >= 1 && x <= row && y >= 1 && y <= col)
	{
		int ret = arr[x - 1][y - 1] + arr[x - 1][y]
			+ arr[x - 1][y + 1] + arr[x][y - 1]
				+ arr[x][y + 1] + arr[x + 1][y - 1]
				+ arr[x + 1][y] + arr[x + 1][y + 1]
				- 8 * '0';
			arr1[x][y] = ret + '0';
			if (arr1[x][y] == '0')
			{
				//检查周围
				if (arr1[x - 1][y - 1] == '*')
				{
					Check_mine_a(arr, arr1, x - 1, y - 1, row, col, mine);
				}
				if (arr1[x - 1][y] == '*')
				{
					Check_mine_a(arr, arr1, x - 1, y, row, col, mine);
				}
				if (arr1[x - 1][y + 1] == '*')
				{
					Check_mine_a(arr, arr1, x - 1, y + 1, row, col, mine);
				}
				if (arr1[x][y - 1] == '*')
				{
					Check_mine_a(arr, arr1, x, y - 1, row, col, mine);
				}
				if (arr1[x][y + 1] == '*')
				{
					Check_mine_a(arr, arr1, x, y + 1, row, col, mine);
				}
				if (arr1[x + 1][y - 1] == '*')
				{
					Check_mine_a(arr, arr1, x + 1, y - 1, row, col, mine);
				}
				if (arr1[x + 1][y] == '*')
				{
					Check_mine_a(arr, arr1, x + 1, y, row, col, mine);
				}
				if (arr1[x + 1][y + 1] == '*')
				{
					Check_mine_a(arr, arr1, x + 1, y + 1, row, col, mine);
				}
			}
	}
	else 
	{
		return;
	}
}
//检查雷
void Check_mine(char arr[ROWS][COLS], char arr1[ROWS][COLS], int row, int col, int mine,int* win)
{
	int x, y = 0;
	printf("请输入您要检查的坐标(例如：x,y)：\n");
	scanf("%d,%d", &x, &y);
	if (1 <= x && x <= row && 1 <= y && y <= col)
	{
		if (arr[x][y] == '1')
		{
			printf("此位置有雷，游戏结束\n");
			for (int i = 1;i <= row;i++)
			{
				for (int j = 1;j <= col;j++)
				{
					if (arr[i][j] == '1')
						arr1[i][j] = '1';
				}
			}
			Print_Board(arr1, ROW, COL);
		}
		else
		{
			if (arr1[x][y] == '*')
			{
				int ret = arr[x - 1][y - 1] + arr[x - 1][y]
					+ arr[x - 1][y + 1] + arr[x][y - 1]
						+ arr[x][y + 1] + arr[x + 1][y - 1]
						+ arr[x + 1][y] + arr[x + 1][y + 1]
						- 8 * '0';
					arr1[x][y] = ret + '0';
					if (arr1[x][y] == '0')
					{
						Check_mine_a(arr, arr1, x, y, row, col, mine);
					}
					Print_Board(arr1, ROW, COL);
					*win = 0;
					for (int i = 0;i < row;i++)
					{
						for (int j = 0;j < col;j++)
						{
							if (arr1[i][j] != '*')
							{
								(*win) = (*win)+1;
							}
						}
					}
					printf("还剩下%d处未检查\n", col * row - *win);
			}
			else
			{
				printf("输入重复或者已经标记，请重新输入:\n");
			}
		}
	}
	else
	{
		printf("输入错误，重新输入\n");
	}
}



//标记雷
void Sign_mine(char arr[ROWS][COLS], char arr1[ROWS][COLS], int row, int col, int mine)
{
	int x, y = 0;
	printf("请输入您要标记的坐标(例如：x,y)：\n");
	scanf("%d,%d", &x, &y);
	if (1 <= x && x <= row && 1 <= y && y <= col)
	{
		if (arr1[x][y] == '*')
		{
			arr1[x][y] = '#';
			Print_Board(arr1, ROW, COL);
		}
		else
		{
			printf("输入错误，重新输入\n");
		}
	}
}

//取消标记
void unSign_mine(char arr[ROWS][COLS], char arr1[ROWS][COLS], int row, int col, int mine)
{
	int x, y = 0;
	printf("请输入您要取消标记的坐标(例如：x,y)：\n");
	scanf("%d,%d", &x, &y);
	if (1 <= x && x <= row && 1 <= y && y <= col)
	{
		if (arr1[x][y] == '#')
		{
			arr1[x][y] = '*';
			Print_Board(arr1, ROW, COL);
		}
		else
		{
			printf("输入错误，重新输入\n");
		}
	}
}

//选择
void Option(char arr[ROWS][COLS], char arr1[ROWS][COLS], int row, int col, int mine)
{
	int win = 0;
	do
	{
		int opp = 0;
		printf("请输入您的选择\n1：扫雷\n2：标记\n3：取消标记\n4：退出\n");
		scanf("%d", &opp);
		switch (opp)
		{
		case 1:
		{
			Check_mine(arr, arr1, ROW, COL, MINE, &win);
			if (win == (row * col) - mine)
			{
				printf("排雷成功！\n");
			}
			break;
		}
		case 2:
		{
			Sign_mine(arr, arr1, ROW, COL, MINE);
			break;
		}
		case 3:
		{
			unSign_mine(arr, arr1, ROW, COL, MINE);
			break;
		}
		case 4:
		{
			win = (row * col) - mine;
			break;
		}
		default:
		{
			printf("输入错误，重新输入！\n");
			break;
		}
		}
	} while (win < (row * col) - mine);
	
}

//扫雷
void game()
{
	printf("游戏开始\n");
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	//初始化两个棋盘
	Init_Board(mine, ROWS, COLS, '0');
	Init_Board(show, ROWS, COLS, '*');
	//Print_Board(mine, ROW, COL);
	Print_Board(show, ROW, COL);
	//布置雷
	Set(mine, ROW, COL, MINE);
	//选择
	Option(mine, show, ROW, COL, MINE);
}